package com.ruyuan.eshop.construction.api.impl;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.common.core.PageResult;
import com.ruyuan.eshop.construction.api.AuditApi;
import com.ruyuan.eshop.construction.domain.dto.AuditInfoDTO;
import com.ruyuan.eshop.construction.domain.dto.DraftDetailDTO;
import com.ruyuan.eshop.construction.domain.dto.ExecAuditDTO;
import com.ruyuan.eshop.construction.domain.request.AuditRequest;
import com.ruyuan.eshop.construction.domain.request.QueryDraftRequest;
import com.ruyuan.eshop.construction.domain.request.QueryTodoListRequest;
import com.ruyuan.eshop.common.exception.ProductBizException;
import com.ruyuan.eshop.construction.service.AuditService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 审批服务
 *
 * @author zhonghuashishan
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = AuditApi.class, retries = 0)
public class AuditApiImpl implements AuditApi {

    @Autowired
    private AuditService auditService;


    @Override
    public JsonResult<PageResult<AuditInfoDTO>> getTodoList(QueryTodoListRequest request) {
        try {
            PageResult<AuditInfoDTO> todoList = auditService.getTodoList(request);
            return JsonResult.buildSuccess(todoList);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<DraftDetailDTO> getDraftDetail(QueryDraftRequest request) {
        try {
            DraftDetailDTO draftDetailDTO = auditService.getDraftDetail(request);
            return JsonResult.buildSuccess(draftDetailDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<ExecAuditDTO> execAudit(AuditRequest request) {
        try {
            ExecAuditDTO execAuditDTO = auditService.execAudit(request);
            return JsonResult.buildSuccess(execAuditDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
